package com.joint.base.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.joint.base.dao.BaseEntityDao;
import com.joint.base.dao.ProcessBriefDao;
import com.joint.base.dao.StepBriefDao;
import com.joint.base.entity.*;
import com.joint.base.service.DutyService;
import com.joint.base.service.PowerService;
import com.joint.base.service.ProcessBriefService;
import com.joint.base.service.StepBriefService;
import com.joint.base.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;


/**
 * Service实现类 - 
 * ============================================================================
 * 版权所有 2013
 * ----------------------------------------------------------------------------
 * 
 * @author 
 * 
 * @version 0.1 2011-6-13
 */

@Service
public class StepBriefServiceImpl extends BaseEntityServiceImpl<StepBrief, String> implements StepBriefService {
    @Resource
    private StepBriefDao stepBriefDao;
    @Resource
    private PowerService powerService;
    @Resource
    private DutyService dutyService;

    @Override
    public BaseEntityDao<StepBrief, String> getBaseEntityDao() {
        return stepBriefDao;
    }

    @Override
    public StepBrief getInfoByStepNo(ProcessBrief processBrief, int stepNo) {
        return stepBriefDao.getInfoByStepNo(processBrief, stepNo);
    }

    @Override
    public StepBrief getInfoByCommonConfig(CommonConfig commonConfig) {
        return stepBriefDao.getInfoByCommonConfig(commonConfig);
    }

    @Override
    public List<Users> findSpecailByUser(StepBrief stepBrief, Duty duty) {
        List<Users> usersList = Lists.newArrayList();
        String strSpecialType = stepBrief.getSpecialType();
        if(strSpecialType != null){
            if(strSpecialType.indexOf("1") != -1){
                Set<Duty> dutySet = powerService.findParentByPower(duty);
                for(Duty duty1 : dutySet){
                    usersList.add(duty1.getUsers());
                }
            }
            if(strSpecialType.indexOf("2")!= -1){
                Duty duty1 = dutyService.getPrincipalDuty(duty.getDepartment());
                if(duty1 != null){
                    usersList.add(duty1.getUsers());
                }
            }
            if(strSpecialType.indexOf("3")!= -1){
                Duty duty1 = dutyService.getParentPrincipal(duty.getDepartment());
                if(duty1 != null){
                    usersList.add(duty1.getUsers());
                }
            }
        }
        return usersList ;
    }

    @Override
    public Set<Duty> findSpecialByDuty(StepBrief stepBrief, Duty duty) {
        Set<Duty> dutySet = Sets.newHashSet();
        String strSpecialType = stepBrief.getSpecialType();
        if(strSpecialType != null){
            if(strSpecialType.indexOf("1") != -1){
                dutySet.addAll(powerService.findParentByPower(duty));

            }
            if(strSpecialType.indexOf("2")!= -1){
                Duty duty1 = dutyService.getPrincipalDuty(duty.getDepartment());
                if(duty1 != null){
                    dutySet.add(duty1);
                }
            }
            if(strSpecialType.indexOf("3")!= -1){
                Duty duty1 = dutyService.getParentPrincipal(duty.getDepartment());
                if(duty1 != null){
                    dutySet.add(duty1);
                }
            }
        }
        return dutySet;
    }
}